#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

// gcc ctrlprocess.c -o ctrlprocess
int main(int argc, char * argv[]) {
    int n;
    char buf[1024];
    FILE *stdinx = fopen("/dev/tty", "rw+");
    int num = fileno(stdinx);
    // close(0);
    int res = dup2(num, 0);
    // printf("res %d\n", res);
    // close(num);
    printf("hello world!\n");
    sleep(2);
    printf("filenossss: %d\n", num);
    while (1)
    {
        printf("loop...\n");
        if((n = read(0, buf, 10)) > 0) {
            buf[n] = '\0';
            printf("get pipe data: %s\n", buf);
            if (!strcmp(buf, "exit")) {
                exit(0);
            }
            if (!strcmp(buf, "getpid")) {
                printf("My pid is %d\n", getpid());
                sleep(3);            
                exit(0);
            }
        }
        sleep(3);
        printf("end\n");
       
    }
    
}